﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Marbles.Core.Controls
{
    [ToolboxData("<{0}:jQPanel runat=server></{0}:jQPanel>")]
    public class jQPanel : Panel
    {
        [Category("jQuery UI")]
        public bool Draggable { get { object obj = ViewState["Draggable"]; if (obj == null) { return false; } return Convert.ToBoolean(obj); } set { ViewState["Draggable"] = value; } }
        [Category("jQuery UI")]
        public bool Droppable { get { object obj = ViewState["Droppable"]; if (obj == null) { return false; } return Convert.ToBoolean(obj); } set { ViewState["Droppable"] = value; } }
        [Category("jQuery UI")]
        public bool Resizable { get { object obj = ViewState["Resizable"]; if (obj == null) { return false; } return Convert.ToBoolean(obj); } set { ViewState["Resizable"] = value; } }
        [Category("jQuery UI")]
        public bool Selectable { get { object obj = ViewState["Selectable"]; if (obj == null) { return false; } return Convert.ToBoolean(obj); } set { ViewState["Selectable"] = value; } }
        [Category("jQuery UI")]
        public bool Sortable { get { object obj = ViewState["Sortable"]; if (obj == null) { return false; } return Convert.ToBoolean(obj); } set { ViewState["Sortable"] = value; } }


        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);

            if (!Draggable && !Droppable && !Resizable && !Selectable && !Sortable) { return; }

            writer.AddAttribute("type", "text/javascript");
            writer.AddAttribute("language", "javascript");
            writer.RenderBeginTag(HtmlTextWriterTag.Script);

            if (Draggable)
            {
                writer.Write(string.Format("$(\"#{0}\" ).draggable();", this.ClientID));
            }

            if (Droppable)
            {
                writer.Write(string.Format("$(\"#{0}\" ).droppable();", this.ClientID));
            }

            if (Resizable)
            {
                writer.Write(string.Format("$(\"#{0}\" ).resizable();", this.ClientID));
            }

            if (Selectable)
            {
                writer.Write(string.Format("$(\"#{0}\" ).selectable();", this.ClientID));
            }

            if (Sortable)
            {
                writer.Write(string.Format("$(\"#{0}\" ).sortable();", this.ClientID));
            }

            writer.RenderEndTag();
        }
    }
}
